<?php 
namespace Admin\Controller;
use Think\Controller;

class LbimgController extends AdminController
{
	public function index()
	{
		$data=D('lbimg')->select();
		// var_dump($data);exit;
		$this->assign('list',$data);
		$this->display();	
	}

	public function add()
	{
		$this->display();	
	}

	public function doadd()
	{
		$lbimg=D('lbimg');
        if(!empty($_FILES)){
			//上传单个图像
			$upload=new \Think\Upload();
	    	$upload->maxSize=3145678;
	    	$upload->exts=array('jpg','gif','png','jpeg');
	    	$upload->rootPath='Uploads/';
	    	$upload->savePath='';
	    	$upload->saveName='com_create_guid';
	    	$upload->autoSub=true;
	        $upload->subName = array('date','Ymd');
	    	$info=$upload->uploadOne($_FILES['img']);
	    	if (!$info) {
	    		$this->error($upload->getError());
	    	} else {
	    		$filepath = $info['savepath'].$info['savename'];
	    		$data['img']=$filepath;
	    		// $data['gid']=I('post.gid/d');
	    		
	    		$lbimg->create($data);
	    		$result=$lbimg->add();
		    	    if(!$result){
		    			$this->error('上传失败!');
		    		}else{
			    		$this->success('上传成功!',U('index'));
	    		    }	
		    	}
			}	
		}

		public function edit()
		{
            $id=I('get.id/d');
            $data = M('lbimg')->find($id);
	        $this->assign('data',$data);
	        $this->display('edit');
		}

		// public function update()
		// {
		// 	// $lbimg=D('lbimg');
		// 	if (empty($_POST)) {
	 //            $this->error('请填写数据!',U('User/edit'));
	 //            exit;
	 //        }
	 //        M('lbimg')->create();
	 //        // echo M('lbimg')->getLastSql().'<br>';exit;
	 //        if (M('lbimg')->save()) {
	 //            $this->success('修改成功',U('index'));
	 //        } else {
	 //            $this->error('修改失败');
	 //        }
		// }

		//执行修改
	    public function update()
	    {
	    	// var_dump($_POST);exit;
	        if (empty($_POST)) {
	            $this->error('请填写数据!',U('lbimg/edit'));
	            exit;
	        }
	        $id=I('post.id/d');
	        $lbimg=M('lbimg');
	       
	        // var_dump($_FILES);exit;
	        // $path="./Uploads/";
	        if(!empty($_FILES)){
				//上传单个图像
				$upload=new \Think\Upload();
		    	$upload->maxSize=3145678;
		    	$upload->exts=array('jpg','gif','png','jpeg');
		    	$upload->rootPath='Uploads/';
		    	$upload->savePath='';
		    	$upload->saveName='com_create_guid';
		    	$upload->autoSub=true;
		        $upload->subName = array('date','Ymd');
		    	$info=$upload->uploadOne($_FILES['img']);
		    	// var_dump($info);exit;
		    	if (!$info) {
		    		$this->error($upload->getError());
		    	} else {
		    		$filepath = $info['savepath'].$info['savename'];
		    		$data['img']=$filepath;
		    		$lbimg->create();
		    		$result=$lbimg->where('id='.$id)->save($data);
			    	    if(!$result){
			    			$this->error('上传失败!');
			    		}else{
				    		$this->success('上传成功!',U('index'));
		    		    }	
			    	}
				}	
	    }    

		public function del()
		{
			if (empty($_GET['id'])) {
	            $this->redirect('index');
	            exit;
	        }
	        $id = I('get.id/d');
	        $img=M('lbimg')->where(array('id'=>$id))->find();
	        // var_dump($img);exit;
	        $path="./Uploads/";
	        // var_dump($path);
	        unlink($path.$img['img']);
	        if (M('lbimg')->delete($id) > 0) {
	            $this->success('恭喜您!删除成功!',U('index'));
	        } else {
	            $this->error('删除失败!',U('index'));
	        }
		}

		public function status()
		{
			$id =  I('get.id/d');
	    	// $gid =  I('get.gid/d');
	    	// $list=M('lbimg')->where(array('gid'=>$gid))->select();
			$data['status'] = $_GET['s'];
			$goods = M('lbimg');
			$goods->where(array('id'=>$id))->save($data);
	        $this->redirect('index');
		}
	}